package com.jfs.security.session;

import com.jfs.security.authentication.SessionManager;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;

import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;

/**
 * @Description 会话管理者
 * @E-mail cwm3412638@163.com
 * @Author cwm
 * @Date 2022/8/8
 */
@Slf4j
public class SessionCleanReactor {
    private final ScheduledExecutorService scheduledExecutorService;


    public SessionCleanReactor(ScheduledExecutorService scheduledExecutorService) {
        this.scheduledExecutorService = scheduledExecutorService;
    }

    /**
     * 延时执行清除会话
     * @param token
     * @param initialDelay
     */
    public void cleanSession(String token, long initialDelay) {
        scheduledExecutorService.schedule(new SessionCleanTask(token), initialDelay, TimeUnit.MILLISECONDS);
    }

    @AllArgsConstructor
    public class SessionCleanTask implements Runnable{
        private String token;

        @Override
        public void run() {
            if (log.isDebugEnabled()){
                log.debug("执行清除会话操作！！！");
            }
            SessionManager.cleanIsLogin(token);
        }
    }
}
